package com.milkTeaServer.controller;

import com.milkTeaServer.bean.Order;
import com.milkTeaServer.dao.CartDao;
import com.milkTeaServer.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderDao dao;

    @RequestMapping("/add")
    @ResponseBody
    public Map addOrder(@RequestBody Order order) {
        Map map = new HashMap();
        Order order1 = dao.saveAndFlush(order);
        if (order1 != null) {
            map.put("success", true);
            map.put("massage", "添加订单成功!");
            map.put("data", order1);
        } else {
            map.put("success", true);
            map.put("massage", "添加订单失败!");
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Map findById(@RequestBody Order order) {
        Map map = new HashMap();
        List<Order> orders = dao.findOrderById(order.getId());
        if (orders != null) {
            if (orders.isEmpty()) {
                map.put("success", true);
                map.put("massage", "查询订单成功!");
                map.put("data", null);
            } else {
                map.put("success", true);
                map.put("massage", "查询订单成功!");
                map.put("data", orders.get(0));
            }
        } else {
            map.put("success", true);
            map.put("massage", "查询订单失败!");
            map.put("data", null);
        }
        return map;
    }
}
